package com.hzqy.service.hotel;

import com.hzqy.persistence.dao.hotel.HotelAdvertiseDao;
import com.hzqy.persistence.dao.hotel.HotelUserNeedsDao;
import com.hzqy.service.content.ContentServiceBean;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmAdvertiseStrategyVo;
import com.hzqy.web.vo.CmAdvertiseVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.HotelUserNeedsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HotelAdvertiseServiceBean extends GenericServiceBean<CmAdvertiseVo, HotelAdvertiseDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private HotelAdvertiseDao hotelAdvertiseDao;
	@Override
	public HotelAdvertiseDao getEntityDao() {
		return hotelAdvertiseDao;
	}
	@Resource
	private ContentServiceBean contentServiceBean;
	
	/**
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectHotelAdvertiseList(CmAdvertiseVo cmAdvertiseVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = hotelAdvertiseDao.selectHotelAdvertiseCount(cmAdvertiseVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_ca_position", cmAdvertiseVo.getF_ca_position());
			map.put("f_ca_type", cmAdvertiseVo.getF_ca_type());
			map.put("f_ca_onlinedate", cmAdvertiseVo.getF_ca_onlinedate());
			map.put("f_ca_offlinedate", cmAdvertiseVo.getF_ca_offlinedate());
			map.put("f_ca_status", cmAdvertiseVo.getF_ca_status());
			map.put("f_cc_gg_status", cmAdvertiseVo.getF_cc_gg_status());
			map.put("f_ca_cid", cmAdvertiseVo.getF_ca_cid());
			map.put("f_ca_adsid", cmAdvertiseVo.getF_ca_adsid());
			map.put("f_ca_admid", cmAdvertiseVo.getF_ca_admid());

			List<CmAdvertiseVo> results = hotelAdvertiseDao.selectHotelAdvertiseList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmAdvertiseVo>());
		}
		return map;
	}

	public CmAdvertiseVo selectAdvertiseById(int f_ca_id){
		return hotelAdvertiseDao.selectAdvertiseById(f_ca_id);
	}

	public int deleteAdvertise(CmAdvertiseVo cmAdvertiseVo){
		return hotelAdvertiseDao.deleteAdvertise(cmAdvertiseVo);
	}

	public int insertAdvertise(CmAdvertiseVo cmAdvertiseVo){
		return hotelAdvertiseDao.insertAdvertise(cmAdvertiseVo);
	}

	public int updateAdvertise(CmAdvertiseVo cmAdvertiseVo){
		return hotelAdvertiseDao.updateAdvertise(cmAdvertiseVo);
	}

	/**
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectAdvertiseStrategyList(CmAdvertiseStrategyVo cmAdvertiseStrategyVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = hotelAdvertiseDao.selectAdvertiseStrategyCount(cmAdvertiseStrategyVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_cas_advertiseid", cmAdvertiseStrategyVo.getF_cas_advertiseid());
			map.put("f_cas_name", cmAdvertiseStrategyVo.getF_cas_name());
			map.put("f_cas_areacode", cmAdvertiseStrategyVo.getF_cas_areacode());
			map.put("f_ca_status", cmAdvertiseStrategyVo.getF_cas_status());
			List<CmAdvertiseStrategyVo> results = hotelAdvertiseDao.selectAdvertiseStrategyList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmAdvertiseStrategyVo>());
		}
		return map;
	}

	public CmAdvertiseStrategyVo selectAdvertiseStrategyById(int f_cas_id){
		return hotelAdvertiseDao.selectAdvertiseStrategyById(f_cas_id);
	}

	public List<CmAdvertiseVo> selectAdvertiseByOrderNumber(String f_ca_order_number){
		return hotelAdvertiseDao.selectAdvertiseByOrderNumber(f_ca_order_number);
	}

	public List<CmAdvertiseVo> selectAdvertiseByStatus(){
		return hotelAdvertiseDao.selectAdvertiseByStatus();
	}

	public int deleteAdvertiseStrategy(int f_cas_id){
		return hotelAdvertiseDao.deleteAdvertiseStrategy(f_cas_id);
	}

	public int insertAdvertiseStrategy(CmAdvertiseStrategyVo cmAdvertiseStrategyVo){
		return hotelAdvertiseDao.insertAdvertiseStrategy(cmAdvertiseStrategyVo);
	}

	public int updateAdvertiseStrategy(CmAdvertiseStrategyVo cmAdvertiseStrategyVo){
		return hotelAdvertiseDao.updateAdvertiseStrategy(cmAdvertiseStrategyVo);
	}

	public int updateAdvertiseStrategyStatus(CmAdvertiseStrategyVo cmAdvertiseStrategyVo){
		return hotelAdvertiseDao.updateAdvertiseStrategyStatus(cmAdvertiseStrategyVo);
	}

	public int updateAdvertiseStatus(String ids ,int f_ca_status){
		int flag = 0;
		String[] idsArr = ids.split(",");
		String contentIds="";
		for(int i=0;i<idsArr.length;i++) {
			CmAdvertiseVo cmAdvertiseVo = new CmAdvertiseVo();
			cmAdvertiseVo.setF_ca_id(Integer.valueOf(idsArr[i]));
			cmAdvertiseVo.setF_ca_status(f_ca_status);
			flag += hotelAdvertiseDao.deleteAdvertise(cmAdvertiseVo);
			if(flag>0) {
				CmAdvertiseVo cmAdvertise = hotelAdvertiseDao.selectAdvertiseById(Integer.valueOf(idsArr[i]));
				if (!contentIds.contains(String.valueOf(cmAdvertise.getF_ca_contentid_hd()))) {
					contentIds = contentIds + cmAdvertise.getF_ca_contentid_hd() + ",";
				}
				if (!contentIds.contains(String.valueOf(cmAdvertise.getF_ca_contentid_sd()))) {
					contentIds = contentIds + cmAdvertise.getF_ca_contentid_sd() + ",";
				}
			}
		}
		contentIds = contentIds.substring(0, contentIds.length() - 1);
		if (f_ca_status == 2) {
			flag += contentServiceBean.batchUnReviewContent(contentIds);
		} else if (f_ca_status == 1) {
			flag += contentServiceBean.batchReviewContent(contentIds);
		}
		return flag;
	}
}
